package cn.stylefeng.guns.modular.business.controller;

import cn.stylefeng.guns.modular.business.entity.TrainingRecordRequest;
import cn.stylefeng.guns.modular.business.service.TrainingRecordService;
import cn.stylefeng.roses.kernel.resource.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.resource.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@ApiResource(name = "培训记录管理")
public class TrainingController {

    @Resource
    private TrainingRecordService trainingRecordService;

    /**
     * 分页查询培训记录
     */
    @GetResource(name = "分页查询培训记录", path = "/training/page")
    public ResponseData page(TrainingRecordRequest trainingRecordRequest) {
        return new SuccessResponseData(trainingRecordService.findPage(trainingRecordRequest));
    }

    /**
     * 添加
     */
    @PostResource(name = "提交培训记录", path = "/training/add")
    public ResponseData add(@RequestBody TrainingRecordRequest trainingRecordRequest) {
        try {
            trainingRecordService.add(trainingRecordRequest);
            return new SuccessResponseData();
        } catch (Exception e) {
            log.error("提交培训记录失败！， request：{}", trainingRecordRequest, e);
            return new ErrorResponseData("-1", "提交培训记录失败！");
        }
    }

    /**
     * 添加
     */
    @PostResource(name = "修改培训记录", path = "/training/edit")
    public ResponseData edit(@RequestBody TrainingRecordRequest trainingRecordRequest) {
        if (trainingRecordRequest.getRecordId() == null) {
            return new ErrorResponseData("-1", "培训记录id不能为空");
        }

        try {
            trainingRecordService.edit(trainingRecordRequest);
            return new SuccessResponseData();
        } catch (Exception e) {
            log.error("提交培训记录失败！， request：{}", trainingRecordRequest, e);
            return new ErrorResponseData("-1", "提交培训记录失败！");
        }
    }

    /**
     * 查询培训记录
     */
    @GetResource(name = "查询培训记录", path = "/training/detail")
    public ResponseData detail(Long recordId) {
        if (recordId == null) {
            return new ErrorResponseData("-1", "培训记录id不能为空");
        }
        return new SuccessResponseData(trainingRecordService.findOneById(recordId));
    }

    /**
     * 添加
     */
    @PostResource(name = "培训记录审批", path = "/training/audit")
    public ResponseData audit(@RequestBody TrainingRecordRequest trainingRecordRequest) {
        if (trainingRecordRequest.getRecordId() == null) {
            return new ErrorResponseData("-1", "培训记录id不能为空");
        }

        try {
            trainingRecordService.audit(trainingRecordRequest);
            return new SuccessResponseData();
        } catch (Exception e) {
            log.error("提交培训记录失败！， request：{}", trainingRecordRequest, e);
            return new ErrorResponseData("-1", "提交培训记录失败！");
        }
    }

}
